package com.bw.controller;

import com.bw.entity.Dict;
import com.bw.entity.HouseAddParam;
import com.bw.entity.HouseInfo;
import com.bw.entity.HouseQuery;
import com.bw.mapper.HouseMapper;
import com.bw.service.HouseService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.spring.kun.common.ZzyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Controller
public class ListController {


    @Autowired
    HouseMapper houseMapper;

    @Autowired
    HouseService houseService;

    @RequestMapping("/list.do")
    public String list(HouseQuery query, @RequestParam(defaultValue = "1") Integer pageNum, Model model) {
        //1. 渲染下拉框  省份
        List<Dict> provinceList = houseMapper.listProvince();
        model.addAttribute("provinceList", provinceList);

        //2. 开始分页查询
        PageHelper.startPage(pageNum, 3);
        List<HouseInfo> list = houseMapper.selectPage(query);
        PageInfo<HouseInfo> pageInfo = new PageInfo<>(list);
        model.addAttribute("pageInfo", pageInfo);

        //3. 条件回显

        model.addAttribute("query", query);

        return "list";
    }


    @RequestMapping("/addPage.do")
    public String addPage(Model model) {
        //1. 渲染下拉框  省份
        List<Dict> provinceList = houseMapper.listProvince();
        model.addAttribute("provinceList", provinceList);
        return "add";

    }

    @RequestMapping("/add.do")
    public String add(@Valid HouseAddParam addParam, BindingResult result, Model model) throws IOException {
        if (result.hasErrors()) {
            model.addAttribute("error", result.getAllErrors().get(0).getDefaultMessage());
            //1. 渲染下拉框  省份
            List<Dict> provinceList = houseMapper.listProvince();
            model.addAttribute("provinceList", provinceList);
            return "add";
        }
        // 处理图片
        MultipartFile picFile = addParam.getPicFile();
        String filename = picFile.getOriginalFilename();// 获取原图片的名字

        String uuid = UUID.randomUUID().toString(); // 生成随机的图片名

        String newFileName = uuid + "-" + filename;
        // 把这个图片 存到   webapp/static/ 目录下
        picFile.transferTo(new File("/Users/hucongcong/Documents/bawei/season2/2112A/month2-15/web/src/main/webapp/static/" + newFileName));

        // 构造 这个图片的访问路径
        String url = "http://localhost:8080/static/" + newFileName;
        addParam.setUrl(url);

        // 保存楼盘信息
        houseService.addHouse(addParam);
        // 发送短信  告诉管理员 用户在什么时间添加了楼盘信息
        ZzyUtils.sendSms("17611221735",new Date() + "添加了楼盘信息",5);

        return "redirect:list.do";

    }

    @RequestMapping("/getCity.do")
    @ResponseBody
    public List<Dict> getCity(Integer provinceId) {

        return houseMapper.getCity(provinceId);

    }
}
